package imoocvideodev.controller;

import imoocvideodev.common.util.FileUtil;
import imoocvideodev.common.result.IMoocJSONResult;
import imoocvideodev.pojo.Users;
import imoocvideodev.pojo.UsersReport;
import imoocvideodev.service.UsersService;
import imoocvideodev.vo.PublisherVideo;
import imoocvideodev.vo.UsersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Redi
 * @Date: 2021/2/4 11:02
 * @Description: 用户相关接口的控制器
 */
@Api(value = "用户服务的接口",tags = {"用户服务的controller"})
@RestController
@RequestMapping("/user")
public class UserController extends BasicController {

    private static final String UPLOAD_PATH =  "D:/tmpCode/imooc-video-user-data";

    @Autowired
    UsersService usersService;

    @ApiOperation(value = "用户上传头像",notes = "用户上传头像的接口")
    @ApiImplicitParam(name = "userId",value = "用户ID",required = true,dataType = "String", paramType = "query")
    @PostMapping("/uploadFace.do")
    public IMoocJSONResult uploadFace(String userId, @RequestParam("file") MultipartFile file) {
        if (StringUtils.isBlank(userId)){
            return IMoocJSONResult.errorMsg("userId不能为空");
        }
        //无服务器前缀路径 uploadPathDB
        String uploadPathDB =  "/" + userId + "/face";

        //绝对路径 faceImagePath
        String faceImagePath = null;
        try{
            faceImagePath = FileUtil.uploadFile(file,uploadPathDB);
            Users updatedUsers = new Users();

            //返回的地址是经过MVC映射的，故只能返回 UPLOAD_PATH 后面的那一串字符。
            String[] returnImagePaths = faceImagePath.split(UPLOAD_PATH);
            uploadPathDB = returnImagePaths[1];

            updatedUsers.setId(userId);
            updatedUsers.setFaceImage(uploadPathDB);
            usersService.update(updatedUsers);
        }
        catch (Exception e){
            return IMoocJSONResult.errorMsg("上传出错");
        }

        //分割后的字符串，第二串才是要返回的！
        return IMoocJSONResult.okData(uploadPathDB);
    }

    @ApiOperation(value = "用户更新信息",notes = "用户更新信息的接口")
    @ApiImplicitParam(name = "usersId",value = "用户ID",required = true,dataType = "String", paramType = "query")
    @PostMapping("/update.do")
    public IMoocJSONResult updateUserInfo(@RequestBody Users users){
        if (users.getId() == null){
            return IMoocJSONResult.errorMsg("用户ID不能为空！");
        }
        return usersService.update(users);
    }

    @ApiOperation(value = "查询用户信息",notes = "查询用户信息的接口")
    @ApiImplicitParam(name = "usersId",value = "用户ID",required = true,dataType = "String", paramType = "query")
    @PostMapping("/findById.do")
    public IMoocJSONResult updateUserInfo(@RequestParam String usersId,
                                          @RequestParam String fanId){
        if (usersId == null){
            return IMoocJSONResult.errorMsg("用户ID不能为空！");
        }
        Users usersInfo = usersService.selectUsersById(usersId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(usersInfo,usersVO);

        usersVO.setFollow(usersService.checkIfFollow(usersId,fanId));

        return IMoocJSONResult.build(200,"查找用户成功！",usersVO);

    }

    @ApiOperation(value = "获取视频发布者与当前用户的关系信息", notes = "获取视频发布者与当前用户的关系信息的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginUserId", value = "当前登录的用户id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "videoId", value = "当前视频id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "publishUserId", value = "当前视频发布者id", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/getPublisher.do")
    public IMoocJSONResult getPublisher(String loginUserId, String videoId, String publishUserId){
        if (StringUtils.isAnyBlank(videoId,publishUserId)){
            return IMoocJSONResult.errorMsg("传入的视频id或视频发布者id不可为空");
        }

        //1.查询视频发布者信息
        Users userInfo = usersService.selectUsersById(publishUserId);
        UsersVO publisher = new UsersVO();
        BeanUtils.copyProperties(userInfo,publisher);

        //2.查询当前登录者与视频点赞的关系
        boolean userLikeVideo =usersService.isUserLikeVideo(loginUserId,videoId);

        //3.合并为vo返回
        PublisherVideo publisherVideo = new PublisherVideo();
        publisherVideo.setPublisher(publisher);
        publisherVideo.setUsersLikeVideos(userLikeVideo);

        return IMoocJSONResult.okData(publisherVideo);
    }

    @PostMapping("/beFans")
    @ApiOperation(value = "用户关注", notes = "用户关注的接口")
    public IMoocJSONResult beFans(String userId, String fanId){
        if (StringUtils.isAnyBlank(userId,fanId)){
            return IMoocJSONResult.errorMsg("传入的userId与fanId不可为空");
        }
        return usersService.beFans(userId, fanId);
    }

    @PostMapping("/dontBeFans")
    @ApiOperation(value = "用户取关", notes = "用户取关的接口")
    public IMoocJSONResult dontBeFans(String userId, String fanId){
        if (StringUtils.isAnyBlank(userId,fanId)){
            return IMoocJSONResult.errorMsg("传入的userId与fanId不可为空");
        }
        return usersService.dontBeFans(userId, fanId);
    }

    @PostMapping("/reportUser.do")
    @ApiOperation(value = "举报视频", notes = "举报视频的接口")
    public IMoocJSONResult reportUser(@RequestBody UsersReport usersReport){
        return usersService.reportUsers(usersReport);
    }

}
